package com.example.filmsearchapplication.utils;

import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.example.filmsearchapplication.MainActivity;
import com.example.filmsearchapplication.R;
import com.example.filmsearchapplication.model.Film;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class DataManager {
    private static DataManager dataManager;

    private ArrayList<Film> filmList = new ArrayList<Film>();
    private String fileName = "filmData";
    private File sdCardDir;

    private DataManager(){
        if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            Toast.makeText(MainActivity.getContext(),"手机没有SD卡！",Toast.LENGTH_SHORT).show();
        }
        sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
        initData();
    }

    public static DataManager getDefaultDataManager(){
        if(dataManager==null)
            dataManager = new DataManager();
        return dataManager;
    }

    private void initData() {
        filmList.add(new Film("诗无尽头",   "亚历桑德罗·佐杜洛夫斯基","传记",    2016,false, R.drawable.picture_one));
        filmList.add(new Film("玛戈王后",   "帕特里斯·夏侯",        "爱情-传记",1994,false, R.drawable.picture_two));
        filmList.add(new Film("党同伐异",   "大卫·格里菲斯",        "爱情",    1916,false, R.drawable.picture_three));
        filmList.add(new Film("信条",     "克里斯托弗·诺兰",       "科幻",    2020,true,R.drawable.picture_four));
        filmList.add(new Film("我是大哥大","福田雄一",           "喜剧",   2020,true,R.drawable.picture_five));
        filmList.add(new Film("行骗天下","田中亮",             "喜剧-悬疑",2019,true,R.drawable.picture_six));
        filmList.add(new Film("波西米亚狂想曲","布莱恩·辛格",   "传记",   2018,true,R.drawable.picture_seven));
        filmList.add(new Film("情人",     "让·雅克·阿诺",      "爱情-传记",1992,true,R.drawable.picture_eight));
        filmList.add(new Film("色戒",     "李安",               "爱情",2007,true,R.drawable.picture_nine));
        filmList.add(new Film("银翼杀手","雷德利·斯科特",     "科幻",      1982,true,R.drawable.picture_ten));
        filmList.add(new Film("月球",     "邓肯·琼斯",        "科幻-悬疑",2009,true,R.drawable.picture_eleven));
        filmList.add(new Film("我杀了我妈妈","泽维多·多兰",    "传记",       2009,true,R.drawable.picture_twelve));
        filmList.add(new Film("黑店狂想曲","让·皮埃尔·热内",   "喜剧",       1992,true,R.drawable.picture_thirteen));
        filmList.add(new Film("断背山",    "李安",           "爱情",       2005,true,R.drawable.picture_fourteen));
        filmList.add(new Film("怪宴",     "罗伯特·摩尔",       "喜剧-悬疑",1976,false,R.drawable.picture_fifteen));
        filmList.add(new Film("盲山",     "李杨",           "传记",       2007,true,R.drawable.picture_sixteen));
    }

    public ArrayList<Film> getFilmList() {
        return filmList;
    }

    public void writeBookByStream(){
        try{
            File saveFile = new File(sdCardDir,fileName);
            FileOutputStream fout = new FileOutputStream(saveFile);
            ObjectOutputStream Oout = new ObjectOutputStream(fout);
            for(Film film :filmList) {
                Oout.writeObject(film);
            }
            Oout.close();
            fout.close();
        }catch (Exception e){
            Log.d("<Test>",e.toString());
        }
    }

    public void readBookByStream() throws IOException {
        filmList.clear();
        FileInputStream fin = null;
        ObjectInputStream oin = null;
        try {
            File saveFile = new File(sdCardDir,fileName);
            fin = new FileInputStream(saveFile);
            oin = new ObjectInputStream(fin);
            while(true) {
                filmList.add((Film)oin.readObject());
            }
        }catch(EOFException e) {
            Log.d("<Test>","已读到文件末尾");
        }catch (Exception e) {

        }finally {
            oin.close();
            fin.close();
        }
    }
}
